<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if(isset($_SESSION["Username"]))
{
    include("../models/webconfig.php");
    include("../models/class.connection.php");
    include("../models/entities/class.ProductEntity.php");
    include("../models/repositories/class.ProductRepository.php");
    include("../library/library.pager.php");
    
    include("../models/repositories/class.GroupAuthorityRepository.php");
    if(isset($_SESSION['Account'])){
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if(!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 2))
            header("location:NoAccess.php");
    }
    else
    {
        header("location:NoAccess.php");
    }

    $productRepository = new ProductRepository();
    $totalItems = 0;
    $curPage = 1;
    if(isset($_GET['page']))
    {
        if(is_numeric($_GET['page']))
            $curPage = $_GET['page'];
    }
    $results = $productRepository->GetAllPager($curPage, 20, $totalItems);
    $generateLink = GenerateLink($totalItems, 5, 20, $curPage);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Product Manager</title>
<link href="../content/Layout.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="../content/ui-lightness/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" media="screen"/>
<script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../scripts/jquery-ui-1.8.6.custom.min.js"></script>
<script type="text/javascript" src="../scripts/search_backend.js"></script>
<script type="text/javascript" src="../scripts/adminproduct.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var str = location.href.toLowerCase();

        $('ul#mainNav li a').each(function () {
            if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
             $(this).addClass("active");
        });

        $('ul#sideNav li a').each(function () {
            if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
            $(this).addClass("active");
        });
        
        ProductManager.init();
    });
</script>
</head>

<body>
    <div id="format">
        <div id="wrapper">
        	<!--Ph?n main menu-->
			<?php
				include("../includes/backend_mainnav.php");
			?>
		
            <div id="containerHolder">
                <div id="container" style="color: #646464;">
                	<!--Ph?n main content vi?t t?i dây-->
                    <?php
		              include("../includes/searchBox.php");
                    ?>
                    <div class="clear"></div>
                    <h3>Danh sách sản phẩm</h3>
                    <div class="clear"></div>
                    <div style="float: right; margin-right:20px; font-weight: bold;"><a href="CreateProduct.php">Thêm sản phẩm</a></div>
                    <div class="clear"></div>
                    <div class="data" id="productContent" style="background: none; margin-top: 20px; text-align: center; width: 100%; min-height: 100px;">
                        <div class="loading"></div>
                        <div class="pagination pagination-left">
                            <div class="results">
                                <span>
                                    Kết quả từ <?php echo (($curPage - 1)*9 + (count($results) > 0 ? 1 : 0));?> - <?php echo (($curPage - 1)*9 + count($results));?> của <?php echo $totalItems ?>
                                </span>
                            </div>
                            <ul class="pager"><?php echo $generateLink ?></ul>
                        </div>
                        <div class="clearall"></div>
                        <table>
                            <tr>
                                <td>STT</td>
                                <td>Mã SP</td>
                                <td>Tên SP</td>
                                <td>Màu sắc</td>
                                <td>Giá</td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                            <?php
                                for($i = 0; $i < count($results); $i++){
                            ?>
                            <tr>
                                <td><?php echo ($i + 1); ?></td>
                                <td><?php echo $results[$i]->Code_s; ?></td>
                                <td>
                                    <a class="detail" href="#" code="<?php echo $results[$i]->Code_s; ?>" onclick="return false;">
                                        <?php
                                            $arr = explode('|', $results[$i]->ImageSrc_s);                                            
                                        ?>
                                        <img style="max-width: 60px; max-height: 150px;" src="../content/images/Products/<?php echo $results[$i]->Code_s;?>/<?php echo $arr[0];?>" />
                                    </a>
                                    <br />
                                    <a class="detail" href="" code="<?php echo $results[$i]->Code_s; ?>" onclick="return false;"><?php echo $results[$i]->Name_s; ?></a>
                                </td>
                                <td><?php echo $results[$i]->Color_s; ?></td>
                                <td><?php echo number_format($results[$i]->Price_f); ?></td>
                                <td><a class="detail" href="" code="<?php echo $results[$i]->Code_s; ?>" onclick="return false;">Chi tiết</a></td>
                                <td><a href="EditProduct.php?code=<?php echo $results[$i]->Code_s; ?>">Sửa</a></td>
                                <td><a href="#" onclick="return false;" class="deleteProduct" code="<?php echo $results[$i]->Code_s; ?>">Xóa</a></td>
                            </tr>
                            <?php
                                }
                            ?>
                        </table>
                    </div>
                </div>
                <!-- // #main -->
            </div>
            <div id="dialog" title="Sản phẩm"></div>
            <!-- // #container -->
            
            <!--Ph?n footer c?a trang-->
            <?php
            include("../includes/backend_footer.php");
            ?>
        </div>
        </div>
</body>

</html>
<?php
}
else
{
    header("location:../views/Admin_login.php?logout=TRUE");
}
?>
